/**
' Project must reference COM type library "Primavera Portfiolio Management Open API Interface"
'
' This is a part of the Primavera Portfiolio Management API examples.
' Copyright 1998, 2020, Oracle and/or its affiliates. All Rights Reserved.
'
' This source code is only intended as a supplement to the
'   Primavera Portfolio Management Enterprise API
' electronic documentation provided with the product.
'
' This source code is compatible with Primavera Portfolio Management 20.0 
**/

//*************************************************
// Set the 'Domains' value list to show only Items(1; 1.7), Portfolios, Gizmos(3;) , Wizards (; 3.14) , SubItems(;23.1023456789) and categories(;0).  Gizmos(3) will replace Applications(3)
//*************************************************

using System;
using System.Windows.Forms;

// needed for COMException object 
using System.Runtime.InteropServices;

using psPortfoliosLib;

namespace CSharpExamples 
{
        public class CpsPortfoliosValueList_UpdateValues_Example_2
        {
                string mToken;
        
                public void Login() 
                {
                        try
                        {
                                psPortfoliosSecurity psSecurity = new psPortfoliosSecurity();
                                mToken = psSecurity.Login("admin", "Password", 10000); // Never code the password in the source file    
                        }
                        catch (COMException E) 
                        {
                                // in case of exception, print the error info
                                MessageBox.Show("Error " + 
                                                (psRETURN_VALUES)(-E.ErrorCode) +  // negate the number to obtain the real error code
                                                " " + E.Message, 
                                                "Login");
                                                // in a real system, the description is only displayed to support personnel, 
                                                // not to the user
                        }                       
                        catch (Exception E) 
                        {
                                MessageBox.Show("Unexcpected Error: \n" + 
                                                E.ToString(), 
                                                "Login");
                        }                                       
                        
                }
                
                public void psPortfoliosValueList_UpdateValues_Example_2() 
                {

                        try 
                        {

                                psPortfoliosValueList ValueListObj = new psPortfoliosValueList();    

                                ValueListObj.SetSecurity(mToken);


                                #region Prepare vaValues

                                psVariantArrayForInput vvaValues = new psVariantArrayForInput();
                                vvaValues.CreateEmpty();     
                                psPortfoliosValueListValueInfo vaValuesEntry; 
                                object vaValuesObj; // needed to overcome casting issues of c#             
        
                                vaValuesEntry = new psPortfoliosValueListValueInfo();               
                                vaValuesEntry.ID=1;
                                vaValuesEntry.Text=@"Items";
                                vaValuesEntry.Weight=1.7F;
                                vaValuesObj=vaValuesEntry;
                                vvaValues.Add(ref vaValuesObj);
                                
                                
        
                                vaValuesEntry = new psPortfoliosValueListValueInfo();               
                                vaValuesEntry.Text=@"Portfolios";
                                vaValuesObj=vaValuesEntry;
                                vvaValues.Add(ref vaValuesObj);
                                
                                
        
                                vaValuesEntry = new psPortfoliosValueListValueInfo();               
                                vaValuesEntry.ID=3;
                                vaValuesEntry.Text=@"Gizmos";
                                vaValuesObj=vaValuesEntry;
                                vvaValues.Add(ref vaValuesObj);
                                
                                
        
                                vaValuesEntry = new psPortfoliosValueListValueInfo();               
                                vaValuesEntry.Text=@"Wizards";
                                vaValuesEntry.Weight=3.14F;
                                vaValuesObj=vaValuesEntry;
                                vvaValues.Add(ref vaValuesObj);
								
								// 'WeightDouble' property should be used to pass a double value(accepts precision up to 10 digits)
								// 'WeightDouble' accepts only non-zero values.
								//  If both WeightDouble and Weight is passed to the object, then WeightDouble value will be saved in the database if the value provided is non-zero value else Weight value will be saved in the database.
								vaValuesEntry = new psPortfoliosValueListValueInfo();               
                                vaValuesEntry.Text=@"SubItems";
                                vaValuesEntry.WeightDouble=23.1023456789;
                                vaValuesObj=vaValuesEntry;
                                vvaValues.Add(ref vaValuesObj);
								
								// 'Weight' property should be used if the value should be set to '0' as 'WeightDouble' accepts only non-zero values
								vaValuesEntry = new psPortfoliosValueListValueInfo();               
                                vaValuesEntry.Text=@"Categories";
                                vaValuesEntry.Weight=0;
                                vaValuesObj=vaValuesEntry;
                                vvaValues.Add(ref vaValuesObj);
                                
                                object vaValues = vvaValues.GetVariantArray();
                                #endregion Prepare vaValues          

                                psRETURN_VALUES retVal = (psRETURN_VALUES)ValueListObj.UpdateValues(
                                        @"Domains", // sName             
                                        ref vaValues  // vaValues, see above how to create this          
                                        );
                         
                                
                                #region Analyze result    
    
                                if (retVal == psRETURN_VALUES.ERR_OK) 
                                {
                                        MessageBox.Show("Success !", @"Set the 'Domains' value list to show only Items(1; 1.7), Portfolios, Gizmos(3;) , Wizards (; 3.14) , SubItems(;23.1023456789) and categories(;0).  Gizmos(3) will replace Applications(3)");
                                } 
                                else 
                                {
                                        string errorString = ValueListObj.GetErrorString();
                                        MessageBox.Show("Error " + 
                                                        retVal +
                                                        " " + errorString, 
                                                        @"Set the 'Domains' value list to show only Items(1; 1.7), Portfolios, Gizmos(3;) , Wizards (; 3.14) , SubItems(;23.1023456789) and categories(;0).  Gizmos(3) will replace Applications(3)");
                                                        // in a real system, the description is only displayed to support personnel, 
                                                        // not to the user
                                }
                                
                                #endregion Analyze result                               
                                
                        }
                        catch (Exception E) 
                        {
                                MessageBox.Show("Unexcpected Error: \n" + 
                                                E.ToString(),
                                                @"Set the 'Domains' value list to show only Items(1; 1.7), Portfolios, Gizmos(3;) , Wizards (; 3.14) , SubItems(;23.1023456789) and categories(;0).  Gizmos(3) will replace Applications(3)");
                        }                  
                }
        }
}
    

